package com.inspur.security.cbb3.kms.keymanager.containersecret.service.impl;

import com.inspur.security.cbb3.kms.keymanager.containersecret.po.ContainerSecretBean;
import com.inspur.security.cbb3.kms.keymanager.containersecret.repository.ContainerSecretRepository;
import com.inspur.security.cbb3.kms.keymanager.containersecret.service.ContainerSecretService;
import com.inspur.security.cbb3.kms.keymanager.secrets.po.SecretsBean;
import com.inspur.security.cbb3.kms.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: lijunchang
 * @Description: 容器密钥关联关系service实现
 * @Date: 2020/10/19
 */
@Service
@Slf4j
@Transactional(readOnly = true)
public class ContainerSecretServiceImpl implements ContainerSecretService {

    @Autowired
    private ContainerSecretRepository containerSecretRepository;
    /**
     * 进行非对称容器-密钥关联关系保存
     *
     * @param containerId     容器主键
     * @param secretsBeanName 密钥名称
     * @param privateSecretId 私钥主键
     * @param publiSecretId   公钥主键
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveContainerSecrets(String containerId,String secretsBeanName, String privateSecretId, String publiSecretId) {
        ContainerSecretBean privateContainerSecretBean = new ContainerSecretBean();
        ContainerSecretBean publicContainerSecretBean = new ContainerSecretBean();

        privateContainerSecretBean.setContainerId(containerId);
        privateContainerSecretBean.setName(secretsBeanName);
        privateContainerSecretBean.setNote(SecretsBean.NOTE_PRIVATE_KEY);
        privateContainerSecretBean.setSecretId(privateSecretId);
        privateContainerSecretBean.setDeleted(false);
        privateContainerSecretBean.setCreatedTime(DateUtil.getNowDate());

        BeanUtils.copyProperties(privateContainerSecretBean,publicContainerSecretBean);
        publicContainerSecretBean.setNote(SecretsBean.NOTE_PUBLIC_KEY);
        publicContainerSecretBean.setSecretId(publiSecretId);

        containerSecretRepository.save(privateContainerSecretBean);
        containerSecretRepository.save(publicContainerSecretBean);
    }

    /**
     * 根据密钥主键进行关联关系删除
     *
     * @param secretId
     */
    @Override
    public void deleteBySecretUuid(String secretId) {
        if(StringUtils.isBlank(secretId)){
            return;
        }
        List<ContainerSecretBean> list=containerSecretRepository.findBySecretId(secretId);
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        containerSecretRepository.deleteInBatch(list);
    }

    /**
     * 根据容器主键进行关联关系删除
     *
     * @param containerId
     */
    @Override
    public void deleteByContainerId(String containerId) {
        if(StringUtils.isBlank(containerId)){
            return;
        }
        List<ContainerSecretBean> list=containerSecretRepository.findByContainerId(containerId);
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        containerSecretRepository.deleteInBatch(list);
    }
}
